<template>
  <div style="width: 400px;">
    <el-form ref="userForm"  :model="form" label-width="80px" :rules="userRules"  >
      <el-form-item label="原密码" prop="oldPass">
        <el-input v-model="form.oldPass"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPass">
        <el-input v-model="form.newPass"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">提交</el-button>
        <el-button @click="resetForm" type="info">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
name: "UpdateUserPass",
  data() {
    return {
      form: {},
      userRules: {
        oldPass: [
          {required: true, message: '请输入旧密码', trigger: 'blur'},
          {min: 6,max:16,message: '请输入6位到16位字符', trigger: 'blur'}
        ],
        newPass: [
          {required: true, message: '请输入新密码', trigger: 'blur'},
          {min: 6,max:16,message: '请输入6位到16位字符', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    onSubmit() {
      var id = localStorage.getItem("id")
      var token = localStorage.getItem("token")
      this.$refs['userForm'].validate((valid) => {
        if (!valid) {
          return
        } else {
          var id = localStorage.getItem("id")
          this.$http.post("teacher/setUserPass?id="+id+"&oldPass="+
              this.form.oldPass+"&newPass="+this.form.newPass).then(res=>{
            if (res.data.success){
              console.log(res)
              this.$message({
                message: res.data.message,
                type: 'success'
              });
              this.$router.push({ name: 'Login'})
            }else {
              this.$message({
                message: res.data.message,
                type: 'error'
              });
            }

          })
        }
      })
    },
    resetForm: function () {
      this.$refs['userForm'].resetFields();
    }
  }
}
</script>

<style scoped>
.el-input{
  width: 400px;
}
</style>